package edu.pku.sei.modeler.uml.commands.sequence;

import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.Fragments.CombinedFragment;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;

public class UMLCombinedFragmentCreateCommand extends
		NestedElementCreateCommand {

	public UMLCombinedFragmentCreateCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		super(node, hostNode, constraint);
	}

	@Override
	public boolean canExecute() {
		return super.canExecute()
				&& (fatherNode.getModelElement() instanceof Interaction
						|| fatherNode.getModelElement() instanceof CombinedFragment);
	}

	@Override
	protected void updateModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof CombinedFragment) {
			CombinedFragment fragment = (CombinedFragment) fatherNode.getModelElement();
			fragment.getFragment().add(modelElement);
		} else {
			Interaction interaction = (Interaction) fatherNode.getModelElement();
			interaction.getFragment().add(modelElement);
		}
	}

	@Override
	protected void undoModelElement() {
		if (fatherNode.getModelElement() instanceof CombinedFragment) {
			CombinedFragment fragment = (CombinedFragment) fatherNode.getModelElement();
			fragment.getFragment().remove(node.getModelElement());
		} else {
			Interaction interaction = (Interaction) fatherNode.getModelElement();
			interaction.getFragment().remove(node.getModelElement());
		}
	}
	@Override
	protected void updateGraphNode() {
		fatherNode.getSubNodes().add(0,this.node);
		this.node.setParent(fatherNode);
	}
}
